package jDemolitionMan;

import java.util.Random;
import java.util.concurrent.Callable;

import Tools.Utiles;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Pyramid;
import com.jme.util.GameTaskQueueManager;
import com.jmetest.physics.Utils;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.StaticPhysicsNode;
import com.jmex.physics.impl.jbullet.JBulletDynamicPhysicsNode;
import com.jmex.physics.impl.jbullet.JBulletPhysicsSpace;
import com.jmex.physics.impl.ode.DynamicPhysicsNodeImpl;
import com.jmex.physics.impl.ode.OdePhysicsSpace;
import com.jmex.physics.material.Material;

public class Enemy implements Runnable {

	private Node rootNode;
	private boolean[][] mazeData;
	private PhysicsSpace phisicsSpace;
	private StaticPhysicsNode enemy;
	private boolean vivo;
	private int alto = 20;
	private int dist = 1;
	private int ancho = 20;

	public Enemy(PhysicsSpace physicsSpace, boolean[][] mazeData, Node rootNode) {
		enemy = physicsSpace.createStaticNode();
		enemy.setName("enemy");

		this.mazeData = mazeData;
		this.rootNode = rootNode;
		this.phisicsSpace = physicsSpace;

		Box model = new Box("enemy_model", new Vector3f(), 2, 2, 2);
		model.setModelBound(new BoundingBox(new Vector3f(), 2, 2, 2));
		enemy.attachChild(model);

		enemy.generatePhysicsGeometry();
		enemy.setLocalTranslation(55, 2, 55);
		// enemy.setCenterOfMass(new Vector3f(0, -1f, 0));
		// enemy.setMass(10);
		final Material playerMaterial = new Material("player material");
		playerMaterial.setDensity(20);
		enemy.setMaterial(playerMaterial);

		rootNode.attachChild(enemy);
		rootNode.updateRenderState();

		// 
		vivo = true;

	}

	@Override
	public void run() {

		Random rnd = new Random();
		boolean exito = false;
		int rn = 0;
		while (vivo) {
			try {
				rn = rnd.nextInt(4);
				exito = move(rn);
				if (exito) {
					Thread.sleep(1000);
				}

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		GameTaskQueueManager.getManager().update(new Callable<Object>() {
			public Object call() throws Exception {
				enemy.delete();
				return null;

			}
		});

	}

	private boolean move(int i) {
		int[] a = Utiles.calcularPosicionEnTablero(enemy.getLocalTranslation());
		switch (i) {
		case 0:
			if (mazeData[a[0]][(a[1] + dist)] == false) {
				enemy
						.setLocalTranslation(a[0] * 10 + 5, 2,
								(a[1] + 1) * 10 + 5);
				return true;
			}
			break;

		case 1:
			if (mazeData[a[0]][(a[1] - dist)] == false) {
				enemy
						.setLocalTranslation(a[0] * 10 + 5, 2,
								(a[1] - 1) * 10 + 5);
				return true;

			}
			break;

		case 2:

			if (mazeData[(a[0] + dist)][a[1]] == false) {
				enemy.setLocalTranslation((a[0] + 1) * 10 + 5, 2,
						(a[1]) * 10 + 5);
				return true;

			}
			break;
		case 3:
			if (mazeData[(a[0] - dist)][a[1]] == false) {
				enemy.setLocalTranslation((a[0] - 1) * 10 + 5, 2,
						(a[1]) * 10 + 5);
				return true;
			}
			break;
		}

		return false;

	}

	public Spatial getNode() {

		return enemy;
	}

}
